﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.Entities;
using WXYW.DAL;
using WXYW.Model;

namespace WXYW.BLL
{
    public class PostService : RepositoryBase
    {
        public void AddPost(Post newPost)
        {
            InsertEntity(newPost);
        }

        public void UpdatePost(Post post)
        {
            UpdateEntity(post);
        }

        public PostType GetPostType(string postTypeIDStr)
        {
            int postTypeID = Convert.ToInt32(postTypeIDStr);
            return GetEntities<PostType>().Where(p => p.ID == postTypeID).FirstOrDefault();
        }

        public IQueryable<PostType> GetAllPostTypes()
        {
            return GetEntities<PostType>();
        }

        public List<PostModel> GetAllPosts(string keyword)
        {
            List<PostModel> models = new List<PostModel>();

            IQueryable<Post> posts = GetEntities<Post>();
            if (!string.IsNullOrEmpty(keyword))
            {
                posts = posts.Where(p => p.Title.Contains(keyword) || p.Keyword.Contains(keyword) || p.Content.Contains(keyword) || p.TargetType.Contains(keyword));
            }
            foreach (Post post in posts)
            {
                PostModel model = new PostModel();
                User author = GetEntities<User>().Where(u => u.UserID == post.AuthorID).FirstOrDefault();
                model.Author = author.ContactPerson;
                model.Content = post.Content;
                model.ID = post.ID.ToString();
                model.Keyword = post.Keyword;
                if (!post.PostTypeReference.IsLoaded)
                {
                    post.PostTypeReference.Load();
                }
                model.PostType = post.PostType.Name;
                model.PublishDate = post.PublishDate.ToString("yyyy-MM-dd");
                model.Source = post.Source;
                model.Status = post.Status;
                model.Target = post.TargetType;
                model.Title = post.Title;
                models.Add(model);
            }

            return models;
        }

        public List<PostModel> GetAllPosts(int userTypeID, string keyword)
        {
            List<PostModel> models = new List<PostModel>();

            IQueryable<Post> posts = GetEntities<Post>().Where(p=>p.PostSecurity.Where(a=>a.Post.ID == p.ID && a.UserType.ID == userTypeID).Count() > 0).OrderByDescending(p=>p.LastUpdate);
            if (!string.IsNullOrEmpty(keyword))
            {
                posts = posts.Where(p => p.Title.Contains(keyword) || p.Keyword.Contains(keyword) || p.Content.Contains(keyword) || p.TargetType.Contains(keyword));
            }
            foreach (Post post in posts)
            {
                PostModel model = new PostModel();
                User author = GetEntities<User>().Where(u => u.UserID == post.AuthorID).FirstOrDefault();
                model.Author = author.ContactPerson;
                model.Content = post.Content;
                model.ID = post.ID.ToString();
                model.Keyword = post.Keyword;
                if (!post.PostTypeReference.IsLoaded)
                {
                    post.PostTypeReference.Load();
                }
                model.PostType = post.PostType.Name;
                model.PublishDate = post.PublishDate.ToString("yyyy-MM-dd");
                model.Source = post.Source;
                model.Status = post.Status;
                model.Target = post.TargetType;
                model.Title = post.Title;
                models.Add(model);
            }

            return models;
        }

        public List<PostModel> GetPostsForCustomer()
        {
            List<PostModel> models = new List<PostModel>();

            IQueryable<Post> posts = GetEntities<Post>().Where(p => p.TargetType.Equals("客户") && p.Status.Equals("有效")).OrderByDescending(p => p.PublishDate);
            foreach (Post post in posts)
            {
                PostModel model = new PostModel();
                User author = GetEntities<User>().Where(u => u.UserID == post.AuthorID).FirstOrDefault();
                model.Author = author.UserName;
                model.Content = post.Content;
                model.ID = post.ID.ToString();
                model.Keyword = post.Keyword;
                if (!post.PostTypeReference.IsLoaded)
                {
                    post.PostTypeReference.Load();
                }
                model.PostType = post.PostType.Name;
                model.PublishDate = post.PublishDate.ToString("yyyy-MM-dd");
                model.Source = post.Source;
                model.Status = post.Status;
                model.Target = post.TargetType;
                model.Title = post.Title;
                models.Add(model);
            }

            return models;
        }

        public PostModel GetPostModel(string postIDStr)
        {
            int postID = Convert.ToInt32(postIDStr);
            Post post = GetEntities<Post>().Where(p => p.ID == postID).FirstOrDefault();
            PostModel model = new PostModel();
            User author = GetEntities<User>().Where(u => u.UserID == post.AuthorID).FirstOrDefault();
            model.Author = author.ContactPerson;
            model.Content = post.Content;
            model.ID = post.ID.ToString();
            model.Keyword = post.Keyword;
            if (!post.PostTypeReference.IsLoaded)
            {
                post.PostTypeReference.Load();
            }
            model.PostType = post.PostType.Name;
            model.PublishDate = post.PublishDate.ToString("yyyy-MM-dd");
            model.Source = post.Source;
            model.Status = post.Status;
            model.Target = post.TargetType;
            model.Title = post.Title;
            model.PostTypeID = post.PostType.ID.ToString();
            return model;
        }

        public Post GetPost(string postIDStr)
        {
            int postID = Convert.ToInt32(postIDStr);
            Post post = GetEntities<Post>().Where(p => p.ID == postID).FirstOrDefault();
            return post;
        }

        public List<PostModel> SearchPost(string keyword)
        {
            List<PostModel> models = new List<PostModel>();

            IQueryable<Post> posts = GetEntities<Post>();
            if (!string.IsNullOrEmpty(keyword))
            {
                posts = posts.Where(p => p.Title.Contains(keyword) ||
                p.Content.Contains(keyword) || p.Keyword.Contains(keyword) || p.Source.Contains(keyword) ||
                p.Status.Contains(keyword));
            }

            foreach (Post post in posts)
            {
                PostModel model = new PostModel();
                User author = GetEntities<User>().Where(u => u.UserID == post.AuthorID).FirstOrDefault();
                model.Author = author.UserName;
                model.Content = post.Content;
                model.ID = post.ID.ToString();
                model.Keyword = post.Keyword;
                if (!post.PostTypeReference.IsLoaded)
                {
                    post.PostTypeReference.Load();
                }
                model.PostType = post.PostType.Name;
                model.PublishDate = post.PublishDate.ToString("yyyy-MM-dd");
                model.Source = post.Source;
                model.Status = post.Status;
                model.Target = post.TargetType;
                model.Title = post.Title;
                models.Add(model);
            }

            return models;
        }

        public PostModel GetLatestPostModelForCustomer()
        {
            Post post = GetEntities<Post>().Where(p => p.Status.Equals("有效") && p.TargetType.Equals("客户")).OrderByDescending(p => p.PublishDate).FirstOrDefault();
            PostModel model = new PostModel();
            if (post != null)
            {
                User author = GetEntities<User>().Where(u => u.UserID == post.AuthorID).FirstOrDefault();
                model.Author = author.UserName;
                model.Content = post.Content;
                model.ID = post.ID.ToString();
                model.Keyword = post.Keyword;
                if (!post.PostTypeReference.IsLoaded)
                {
                    post.PostTypeReference.Load();
                }
                model.PostType = post.PostType.Name;
                model.PublishDate = post.PublishDate.ToString("yyyy-MM-dd");
                model.Source = post.Source;
                model.Status = post.Status;
                model.Target = post.TargetType;
                model.Title = post.Title;
                model.PostTypeID = post.PostType.ID.ToString();
            }
            return model;
        }

        public IQueryable<PostFile> GetPostFiles(int postID)
        {
            return GetEntities<PostFile>().Where(p => p.Post.ID == postID);
        }

        public long AddPostFile(string fileName, int postID)
        {
            PostFile postFile = new PostFile();
            postFile.FileName = fileName;
            postFile.CreateDate = DateTime.Now;
            postFile.Post = GetEntities<Post>().Where(p => p.ID == postID).FirstOrDefault();
            InsertEntity(postFile);
            return postFile.ID;
        }

        public List<PostSecurityModel> GetPostSecurityModelByPostID(int postID)
        {
            List<PostSecurityModel> postSecurityModels = new List<PostSecurityModel>();
            IQueryable<UserType> userTypes = GetEntities<UserType>();
            foreach (UserType userTypeEntity in userTypes)
            {
                PostSecurityModel postSecurityModel = new PostSecurityModel();
                postSecurityModel.PostID = postID;
                postSecurityModel.UserType = userTypeEntity.Name;
                postSecurityModel.UserTypeID = userTypeEntity.ID;

                PostSecurity postSecurity = GetEntities<PostSecurity>().Where(p => p.Post.ID == postID && p.UserType.ID == userTypeEntity.ID).FirstOrDefault();
                if (postSecurity != null)
                {
                    postSecurityModel.HasPermission = true;
                }
                else
                {
                    postSecurityModel.HasPermission = false;
                }
                postSecurityModels.Add(postSecurityModel);
            }
            return postSecurityModels;
        }

        public void AddPostSecurity(int postID, int userTypeID)
        {
            PostSecurity postSecurity = new PostSecurity();
            postSecurity.Post = GetEntities<Post>().Where(p => p.ID == postID).FirstOrDefault();
            postSecurity.UserType = GetEntities<UserType>().Where(u => u.ID == userTypeID).FirstOrDefault();
            InsertEntity(postSecurity);
        }

        public void DeletePostSecurity(int postID, int userTypeID)
        {
            DeleteEntities<PostSecurity>(a=>a.Post.ID == postID && a.UserType.ID == userTypeID, Guid.Empty);
        }

        public IQueryable<PostSecurity> GetPostsNeedSendNotice()
        {
            return GetEntities<PostSecurity>().Where(p => !p.MailSent.HasValue);
        }

        public void UpdatePostSecurity(PostSecurity postSecurity)
        {
            //PostSecurity security = GetEntities<PostSecurity>().Where(p => p.ID == postSecurity.ID).FirstOrDefault();
            //security.MailSent = true;           
            UpdateEntity(postSecurity);
        }
    }
}
